package rullekake.entities;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedList;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import rullekake.beans.util.JsfUtil;

/**
 * Usersklasse.
 * Holder på alle data og relasjonsinformasjon for users. Består av set/get for alt + equals, hashcode og toString.
 *
 */
@Entity
@Table(name = "USERS")
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"),
    @NamedQuery(name = "Users.findByUsername", query = "SELECT u FROM Users u WHERE u.username = :username"),
    @NamedQuery(name = "Users.findByPassword", query = "SELECT u FROM Users u WHERE u.password = :password")})
public class Users implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "USERNAME")
    private String username;
    @Basic(optional = false)
    @Column(name = "PASSWORD")
    private String password;
    @JoinColumn(name = "PERSON_ID", referencedColumnName = "PERSON_ID")
    @OneToOne(optional = false)
    private Person person;
    @JoinColumn(name = "USERNAME", referencedColumnName = "USERNAME")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "users")
    private Collection<Groups> groups;

    public Collection<Groups> getGroups() {
        return groups;
    }

    public void setGroups(Collection<Groups> groups) {
        this.groups = groups;
    }

    public Users() {
    }

    public Users(String username) {
        this.username = username;
    }

    public Users(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        if(password.equals("")) return;
        this.password = JsfUtil.encryptsha256(password);
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }

    public Boolean getTeacher() { return getGroupType("teacher"); }

    public Boolean getInstAdmin() { return getGroupType("instadmin"); }

    public Boolean getSysAdmin() { return getGroupType("sysadmin"); }
    
    private Boolean getGroupType(String groupType) {
        if (groups == null) return false;
      if (groups.isEmpty()) return false;
        for (Groups g: groups) {
            if (g.getGroupsPK().getGroupName().equals(groupType)) return true;
        }
        return false;        
    }

    public void setTeacher(Boolean bool) { setGroupType(bool, "teacher"); }

    public void setInstAdmin(Boolean bool) { setGroupType(bool, "instadmin"); }

    public void setSysAdmin(Boolean bool) { setGroupType(bool, "sysadmin"); }

    private void  setGroupType(Boolean bool, String groupType) {
        if (groups == null) groups = new LinkedList<Groups>();
        if (bool) {
            if (groups.isEmpty()){
                Groups temp = new Groups(getUsername(), groupType);
                temp.setUsers(this);
                groups.add(temp);
                return;
            }
            for (Groups g: groups) {
                if (g.getGroupsPK().getGroupName().equals(groupType)) return;
            }
            Groups temp = new Groups(getUsername(), groupType);
            temp.setUsers(this);
            groups.add(temp);
            return;
       }
       if (groups.isEmpty()) return;
       Groups f = null;
       for (Groups g: groups) if (g.getGroupsPK().getGroupName().equals(groupType)) f = g;    
       if (f!=null) groups.remove(f);
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (username != null ? username.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Users)) {
            return false;
        }
        Users other = (Users) object;
        if ((this.username == null && other.username != null) || (this.username != null && !this.username.equals(other.username))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "rullekake.entities.Users[username=" + username + "]";
    }

}
